/*
 * Decompiled with CFR 0_102.
 * 
 * Could not load the following classes:
 *  android.app.Activity
 *  android.content.ClipboardManager
 *  android.content.ContentResolver
 *  android.content.Context
 *  android.net.wifi.WifiInfo
 *  android.net.wifi.WifiManager
 *  android.os.Handler
 *  android.os.Vibrator
 *  android.provider.Settings
 *  android.provider.Settings$Secure
 *  android.telephony.TelephonyManager
 *  android.text.TextUtils
 *  android.view.View
 *  android.widget.ScrollView
 *  org.json.JSONObject
 */
package com.jdcjk.util;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Vibrator;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ScrollView;

import org.json.JSONObject;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonMethod {
    private static long lastClickTime;


    public static String castLocationToString(Double longitude, Double latitude, Double altitude, Float speed, Float direction) {
        double lat = latitude;
        double lng = longitude;
        double alt = altitude;
        float spd = speed.floatValue();
        String latStr = String.format("%.7f", lat);
        String lngStr = String.format("%.7f", lng);
        String altStr = String.format("%.2f", alt);
        String speedStr = String.format("%.2f", Float.valueOf(spd));
        String locInfoStr = "<longitude>" + lngStr + "</longitude><latitude>" + latStr + "</latitude><altitude>" + altStr + "</altitude><direction>" + 0 + "</direction><speed>" + speedStr + "</speed>";
        return locInfoStr;
    }

    public static String getRandomString(int length) {
        Random randGen = null;
        Object numbersAndLetters = null;
        if (length < 1) {
            return null;
        }
        if (randGen == null) {
            randGen = new Random();
            numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

        }
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; ++i) {
            randBuffer[i] = ((char[]) numbersAndLetters)[randGen.nextInt(71)];
        }
        return new String(randBuffer);
    }

    public static String getAge(Calendar cBirthday) throws Exception {
        Date birthDay = cBirthday.getTime();
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DATE);
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DATE);
        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    --age;
                }
            } else {
                --age;
            }
        }
        return "" + age + "";
    }

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 800) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public static Calendar StringToCalendar(String value) {
        if (value == null || value.equals("")) {
            return null;
        }
        GregorianCalendar dayc1 = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date daystart = null;
        try {
            daystart = df.parse(value);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        dayc1.setTime(daystart);
        return dayc1;
    }

    public static Calendar StringToCalendar(String value, String format) {
        if (value == null || value.equals("")) {
            return null;
        }
        GregorianCalendar dayc1 = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date daystart = null;
        try {
            daystart = df.parse(value);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        dayc1.setTime(daystart);
        return dayc1;
    }

    public static String CalendarToString(Calendar calendar, String format) {
        if (calendar == null) {
            calendar = Calendar.getInstance();
        }
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(calendar.getTime());
    }

    public static String timeToString(long time, String format) {
        if (time == 0) {
            time = Calendar.getInstance().getTimeInMillis();
        }
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        time *= 1000;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(time);
    }

    public static int CompareTime(String beginCalendarStr, String endCalendarStr) {
        Calendar beginCalendar = CommonMethod.StringToCalendar(beginCalendarStr);
        Calendar endCalendar = CommonMethod.StringToCalendar(endCalendarStr);
        long start = beginCalendar.getTimeInMillis();
        long end = endCalendar.getTimeInMillis();
        return (int) ((end - start) / 1000);
    }

    public static int CompareTime(Calendar beginCalendar, Calendar endCalendar) {
        if (beginCalendar == null || endCalendar == null) {
            return -1;
        }
        long start = beginCalendar.getTimeInMillis();
        long end = endCalendar.getTimeInMillis();
        return (int) ((end - start) / 1000);
    }

    public static long CompareTimeToMillions(Calendar beginCalendar, Calendar endCalendar) {
        if (beginCalendar == null || endCalendar == null) {
            return -1;
        }
        long start = beginCalendar.getTimeInMillis();
        long end = endCalendar.getTimeInMillis();
        return end - start;
    }

    public static String getWeekDay(Calendar c) {
        if (c == null) {
            return "\u5468\u4e00";
        }
        if (Calendar.MONDAY == c.get(Calendar.DAY_OF_WEEK)) {
            return "\u5468\u4e00";
        }
        if (Calendar.TUESDAY == c.get(Calendar.DAY_OF_WEEK)) {
            return "\u5468\u4e8c";
        }
        if (Calendar.WEDNESDAY == c.get(Calendar.DAY_OF_WEEK)) {
            return "\u5468\u4e09";
        }
        if (Calendar.THURSDAY == c.get(Calendar.DAY_OF_WEEK)) {
            return "\u5468\u56db";
        }
        if (Calendar.FRIDAY == c.get(Calendar.DAY_OF_WEEK)) {
            return "\u5468\u4e94";
        }
        if (Calendar.SATURDAY == c.get(Calendar.DAY_OF_WEEK)) {
            return "\u5468\u516d";
        }
        if (Calendar.SUNDAY == c.get(Calendar.DAY_OF_WEEK)) {
            return "\u5468\u65e5";
        }
        return "\u5468\u4e00";
    }

    public static Boolean isSameDay(Calendar b, Calendar e) {
        if (b.get(Calendar.MONTH) == e.get(Calendar.MONTH) && b.get(Calendar.DATE) == e.get(Calendar.DATE)) {
            return true;
        }
        return false;
    }

    public static String findString(int beginAt, String content, String beginString, String endString) {
        int stringBegin = content.indexOf(beginString);
        int stringEnd = content.indexOf(endString);
        if (stringBegin == -1 || stringEnd == -1) {
            return null;
        }
        String result = content.substring(beginAt);
        String retrunResult = result.substring(stringBegin += beginString.length(), stringEnd);
        return retrunResult;
    }

    public static String getTrace(Throwable t) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        return buffer.toString();
    }

    public static String getYearOrMonthOrDayOrHourOrMinuteOrSecond(Calendar cal, int type) {
        int result = cal.get(type);
        return String.valueOf(result);
    }

    public static String getDeviceInfo(Context context) {
        try {
            JSONObject json = new JSONObject();
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String device_id = tm.getDeviceId();
            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            String mac = wifi.getConnectionInfo().getMacAddress();
            json.put("mac", (Object) mac);
            if (TextUtils.isEmpty((CharSequence) device_id)) {
                device_id = mac;
            }
            if (TextUtils.isEmpty((CharSequence) device_id)) {
                device_id = Settings.Secure.getString((ContentResolver) context.getContentResolver(), (String) "android_id");
            }
            json.put("device_id", (Object) device_id);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void Vibrate(Activity activity, long milliseconds) {
        Vibrator vib = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(milliseconds);
    }

    public static void Vibrate(Activity activity, long[] pattern, boolean isRepeat) {
        Vibrator vib = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(pattern, isRepeat ? 1 : -1);
    }

    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
        m = p.matcher((CharSequence) str);
        b = m.matches();
        return b;
    }

    public static boolean isBeidou(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1-9][0-9]{3,7}$");
        m = p.matcher((CharSequence) str);
        b = m.matches();
        return b;
    }

    private static String castToMob(String number) {
        number = number.replace((CharSequence) " ", (CharSequence) "");
        if ((number = number.replace((CharSequence) "-", (CharSequence) "")).startsWith("86")) {
            number = number.substring(2);
        }
        if (number.startsWith("+86")) {
            number = number.substring(3);
        }
        if (number.startsWith("17951")) {
            number = number.substring(5);
        }
        return number;
    }

    public static String formatDataSize(int size) {
        String ret = "";
        if (size < 1048576) {
            ret = "" + size / 1024 + "K";
        } else {
            DecimalFormat df = new DecimalFormat("#.00");
            ret = df.format((double) size / 1048576.0) + "M";
        }
        return ret;
    }

    public static void copy(String content, Context context) {
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText((CharSequence) content.trim());
    }

    public static void scroll2Bottom(final ScrollView scroll, final View inner) {
        Handler handler = new Handler();
        handler.post(new Runnable() {

            @Override
            public void run() {
                if (scroll == null || inner == null) {
                    return;
                }
                int offset = inner.getMeasuredHeight() - scroll.getMeasuredHeight();
                if (offset < 0) {
                    System.out.println("\u5b9a\u4f4d...");
                    offset = 0;
                }
                scroll.scrollTo(0, offset);
            }
        });
    }

    public static double myRound(double d, int n) {
        d *= Math.pow(10.0, n);
        d += 0.5;
        d = (long) d;
        return d /= Math.pow(10.0, n);
    }

    public static String convertToSexagesimal(double num) {
        int du = (int) Math.floor(Math.abs(num));
        double temp = CommonMethod.getdPoint(Math.abs(num)) * 60.0;
        int fen = (int) Math.floor(temp);
        double miao = CommonMethod.getdPoint(temp) * 60.0;
        miao = CommonMethod.myRound(miao, 2);
        if (num < 0.0) {
            return "-" + du + "\u00b0" + fen + "\u2032" + miao + "\u2033";
        }
        return "" + du + "\u00b0" + fen + "\u2032" + miao + "\u2033";
    }

    private static double getdPoint(double num) {
        double d = num;
        int fInt = (int) d;
        BigDecimal b1 = new BigDecimal(Double.toString(d));
        BigDecimal b2 = new BigDecimal(Integer.toString(fInt));
        double dPoint = b1.subtract(b2).floatValue();
        return dPoint;
    }

    public static String getMD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] byteDigest = md.digest();

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < byteDigest.length; offset++) {
                int i = byteDigest[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

}

